#!/bin/dash
#
# all credit goes to http://github.com/mitchweaver/bin
#

# Set term (to make sure tputs are interpreted correctly)
TERM=xterm

ckwm() {
    for i ; do
        pgrep -x $i > /dev/null &&
            { WM="$i" ; return ; }
    done
}

ckterm() {
    for i ; do
        pgrep -x $i > /dev/null &&
            { term="$i" ; return ; }
    done
}

ckwm dwm monsterwm windowchef 2bwm sway \
    i3 catwm subtle openbox gnome xfce \
    awesome cwm twm fvwm fvvwm ratpoison \
    sawfish rio larswm 9wm w9wm herbstluftwm \
    pekwm tinywm xmonad lxde dvtm fluxbox \
    bspwm
ckterm st xterm rxvt urxvt rxvt-unicode 9term \
    gnome-terminal lxterminal alacritty kitty \
    iTerm terminator termite Termite

[ -z "$WM" ] && WM=unknown
[ -z "$term" ] && term=unknown

if type pkg_add > /dev/null 2>&1 ; then
    os=OpenBSD
elif type setup-alpine > /dev/null 2>&1 ; then
    os=Alpine
elif type emerge > /dev/null 2>&1 ; then
    os=Gentoo
elif type crux > /dev/null 2>&1 ; then
    os=CRUX
elif type xbps-query > /dev/null 2>&1 ; then
    os=void
elif type pacman > /dev/null 2>&1 ; then
    os=arch
fi

host="$(hostname)"
kernel="$(uname -r)"
shell="$(basename ${SHELL})"

case $os in
    #OpenBSD) 
    #    machine="$(sysctl -n hw.version)"
    #    packages="$(pkg_info -A | wc -l | sed -e 's/^[ \t]*//')"
    #    uptime="$(uptime | awk -F, '{sub(".*up ",x,$1);print $1}' | sed -e 's/^[ \t]*//')"
    #    ;;
    #Alpine)
    #    packages=$(apk info | wc -l)
    #    uptime="$(uptime -p | sed 's/up //')"
    #    ;;
    #CRUX)
    #    packages=$(pkginfo -i | wc -l)
    #    uptime="$(uptime -p | sed 's/up //')"
    #    ;;
    void)
        packages=$(xbps-query --list-pkgs | wc -l)
        mem=$(free -m | awk '/^Mem/ {print $3}')mb
        ;;
    arch)
        packages=$(pacman -Q | wc -l)
        mem=$(free -m | awk '/^Mem/ {print $3}')mb
esac

case $os in
    void)
bc="$(tput bold)"        # bold
# bc=""        # no bold
c0="$(tput setaf 0)"    # black
c1="$(tput setaf 1)"    # red
c2="$(tput setaf 2)"    # green
c3="$(tput setaf 3)"    # yellow
c4="$(tput setaf 4)"    # blue
c5="$(tput setaf 5)"    # magenta
c6="$(tput setaf 6)"    # cyan
c7="$(tput setaf 7)"    # white
rc="$(tput sgr0)"       # reset
lc="${rc}${c1}"    # labels
nc="${rc}${bc}${c2}"    # user and hostname
ic="${rc}"         # info
fc="${rc}${c1}"         # first color
sc="${rc}${bc}"         # second color
tc="${rc}${bc}${c5}"    # third color
cat << EOF
${fc}                ${nc}${USER}${fc}@${nc}${host}${rc}
${fc}  \\${sc}~~~${fc}/  ${tc}' .${fc}    ${lc}OS: ${ic}${os} ${kernel}${rc}
${fc}   \\_/  \\${sc}~~~${fc}/   ${lc}mem: ${ic}${mem}${rc}
${fc}    |    \\_/    ${lc}packages: ${ic}${packages}${rc}
${fc}  __|__   |     ${lc}shell: ${ic}${shell}${rc}
${fc}        __|__   ${lc}WM: ${ic}${WM}${rc}
${fc}                ${lc}terminal: ${ic}${term}${rc}

EOF
;;
    arch)
bc="$(tput bold)"        # bold
# bc=""        # no bold
c0="$(tput setaf 0)"    # black
c1="$(tput setaf 1)"    # red
c2="$(tput setaf 2)"    # green
c3="$(tput setaf 3)"    # yellow
c4="$(tput setaf 4)"    # blue
c5="$(tput setaf 5)"    # magenta
c6="$(tput setaf 6)"    # cyan
c7="$(tput setaf 7)"    # white
rc="$(tput sgr0)"       # reset
lc="${rc}${c1}"    # labels
nc="${rc}${bc}${c2}"    # user and hostname
ic="${rc}"         # info
fc="${rc}${c1}"         # first color
sc="${rc}${bc}"         # second color
tc="${rc}${bc}${c5}"    # third color
cat << EOF
${fc}                ${nc}${USER}${fc}@${nc}${host}${rc}
${fc}  \\${sc}~~~${fc}/  ${tc}' .${fc}    ${lc}OS: ${ic}${os} ${kernel}${rc}
${fc}   \\_/  \\${sc}~~~${fc}/   ${lc}mem: ${ic}${mem}${rc}
${fc}    |    \\_/    ${lc}packages: ${ic}${packages}${rc}
${fc}  __|__   |     ${lc}shell: ${ic}${shell}${rc}
${fc}        __|__   ${lc}WM: ${ic}${WM}${rc}
${fc}                ${lc}terminal: ${ic}${term}${rc}

EOF
;;
esac

#       _.-;;-._
#'-..-'|   ||   |
#'-..-'|_.-;;-._|
#'-..-'|   ||   |
#'-..-'|_.-''-._|

# \~~~/  ' . 
#  \_/  \~~~/
#   |    \_/ 
# __|__   |  
#       __|__

#    .( * .
# .*  .  ) .
#. . POOF .* .
# '* . (  .) '
#  ` ( . *

#          ,~
#          |\
#         /| \
# ~^~ ^~ /_|__\~^~ ~^~
#~^~^ ~ '======' ~^ ~^~

#           ,;:'`'::'
#        __||
#  _____/LLLL\_
#  \__________"|
#~^~^~^~^~^~^~^~^~^~

#   +
#   A_
#  /\-\
# _||"|_
#~^~^~^~^

#     (
#      )
#    _/
# _({_})_
#  /(o)\

#    _....._
#  .'       `'-.  /
# (-L  A  I  N ~-><
#  '._     _,.-'  \
#     `````
